home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlfind.zip / FSTYPE.C < prev    next >
C/C++ Source or Header  |  1990-06-15  |  4KB  |  183 lines

  1. /* fstype.c -- determine type of filesystems that files are on
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie (djm@ai.mit.edu). */
  19.  
  20. #ifndef MNTENT_MISSING
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <mntent.h>
  25.  
  26. #if !defined(MOUNTED) && defined(MNT_MNTTAB)
  27. #define MOUNTED MNT_MNTTAB
  28. #endif
  29.  
  30. char *strcpy ();
  31.  
  32. int xatoi ();
  33. char *dupstr ();
  34. char *strstr ();
  35. char *xmalloc ();
  36.  
  37. extern int errno;
  38.  
  39. /* Structure for holding a mount table entry. */
  40.  
  41. struct mount_entry
  42. {
  43.   char *me_dir;
  44.   char *me_type;
  45.   dev_t me_dev;
  46.   struct mount_entry *me_next;
  47. };
  48.  
  49. /* Linked list of mounted filesystems. */
  50. static struct mount_entry *mount_list;
  51. #endif
  52.  
  53. /* Read the list of currently mounted filesystems into `mount_list'.
  54.    Add each entry to the tail of the list so that they stay in order.  */
  55.  
  56. void
  57. read_mtab ()
  58. {
  59. #ifndef MNTENT_MISSING
  60.   char *table = MOUNTED;    /* /etc/mtab, usually. */
  61.   char *cp;
  62.   FILE *mfp;
  63.   struct mntent *mnt;
  64.   struct mount_entry *me;
  65.   struct mount_entry *mtail;
  66.  
  67.   /* Start the list off with a dummy entry. */
  68.   me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
  69.   me->me_next = NULL;
  70.   mount_list = mtail = me;
  71.  
  72.   mfp = setmntent (table, "r");
  73.   if (mfp == NULL)
  74.     error (1, errno, "%s", table);
  75.  
  76.   while ((mnt = getmntent (mfp)))
  77.     {
  78.       me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry));
  79.       me->me_dir = dupstr (mnt->mnt_dir);
  80.       me->me_type = dupstr (mnt->mnt_type);
  81.       cp = strstr (mnt->mnt_opts, "dev=");
  82.       if (cp)
  83.     me->me_dev = xatoi (cp + 4);
  84.       else
  85.     me->me_dev = -1;    /* Magic; means not known yet. */
  86.       me->me_next = NULL;
  87.  
  88.       /* Add to the linked list. */
  89.       mtail->me_next = me;
  90.       mtail = me;
  91.     }
  92.  
  93.   if (endmntent (mfp) == 0)
  94.     error (0, errno, "error closing %s", table);
  95.  
  96.   /* Free the dummy head. */
  97.   me = mount_list;
  98.   mount_list = mount_list->me_next;
  99.   free (me);
  100. #endif
  101. }
  102.  
  103. /* Return the type of filesystem that the file described by STATP
  104.    is on.  Return NULL if its filesystem type is unknown. */
  105.  
  106. char *
  107. filesystem_type (statp)
  108.      struct stat *statp;
  109. {
  110. #ifndef MNTENT_MISSING
  111.   struct stat disk_stats;
  112.   struct mount_entry *me;
  113.  
  114.   for (me = mount_list; me; me = me->me_next)
  115.     {
  116.       if (me->me_dev == -1)
  117.     {
  118.       if (stat (me->me_dir, &disk_stats) == 0)
  119.         me->me_dev = disk_stats.st_dev;
  120.       else
  121.         {
  122.           error (0, errno, "%s", me->me_dir);
  123.           me->me_dev = -2;    /* So we won't try and fail repeatedly. */
  124.         }
  125.     }
  126.       if (statp->st_dev == me->me_dev)
  127.     return me->me_type;
  128.     }
  129. #endif
  130.   return 0;
  131. }
  132.  
  133. #ifndef MNTENT_MISSING
  134. /* Return the value of the hexadecimal number represented by CP.
  135.    No prefix (like '0x') or suffix (like 'h') is expected to be
  136.    part of CP. */
  137.  
  138. int
  139. xatoi (cp)
  140.      char *cp;
  141. {
  142.   int val;
  143.   
  144.   val = 0;
  145.   while (*cp)
  146.     {
  147.       if (*cp >= 'a' && *cp <= 'f')
  148.     val = val * 16 + *cp - 'a' + 10;
  149.       else if (*cp >= 'A' && *cp <= 'F')
  150.     val = val * 16 + *cp - 'A' + 10;
  151.       else if (*cp >= '0' && *cp <= '9')
  152.     val = val * 16 + *cp - '0';
  153.       else
  154.     break;
  155.       cp++;
  156.     }
  157.   return val;
  158. }
  159.  
  160. char *
  161. dupstr (s)
  162.      char *s;
  163. {
  164.   return strcpy (xmalloc (strlen (s) + 1), s);
  165. }
  166.  
  167. /* Return address of the first substring SUBSTR in string STRING,
  168.    or NULL if there is none. */
  169.  
  170. char *
  171. strstr (string, substr)
  172.      char *string, *substr;
  173. {
  174.   int length;
  175.  
  176.   length = strlen (substr);
  177.   for (; *string; ++string)
  178.     if (!strncmp (string, substr, length))
  179.       return string;
  180.   return NULL;
  181. }
  182. #endif
  183.